package com.example.administrator.myapplication;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/3/25 0025.
 */
public class TitleBar extends LinearLayout {
    private ImageView mIvLeft;
    private TextView mTvTitle;
    private ImageView mIvRight;
    public TitleBar(Context context){
        super(context);

    }
    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context,R.layout.actionbar,this);
        //找到控件
        mIvLeft = (ImageView) findViewById(R.id.iv_left);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mIvRight = (ImageView) findViewById(R.id.iv_right);
    }
    /**
     * 参一：左边图片的资源id   传-1不需要图片
     * 参二：标题
     * 参三：右边图片的id  传-1不需要图片
     * 参四：事件  传null不需要事件
     */
    public void initActionBar(int leftImgId,String title,int rightImgId,View.OnClickListener onClickListener){
        if(TextUtils.isEmpty(title)){
            throw new TitleBarCannotNullException("标题不能为空");
        }
        //左边的图标
        if(leftImgId == -1 ){
            mIvLeft.setVisibility(INVISIBLE);
        }
        else {
            mIvLeft.setImageResource(leftImgId);
            if(null != onClickListener){
                mIvLeft.setOnClickListener(onClickListener);
            }
        }
        //标题
        mTvTitle.setText(title);
        //rightImage
        if(rightImgId == -1){
            mIvRight.setVisibility(INVISIBLE);
        }
        else {
            mIvRight.setImageResource(rightImgId);
            if(null != onClickListener){
                mIvRight.setOnClickListener(onClickListener);
            }
        }
    }
}
